package com.huadao.cart_ccs.common;

import com.huadao.cart_ccs.R;
import com.huadao.cart_ccs.data.bo.ExperimentStage;
import com.huadao.cart_ccs.greendao.GreenDaoManager;
import com.huadao.cart_ccs.greendao.gen.ExperimentStageDao;
import com.huadao.cart_ccs.main.CartApplication;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 实验阶段管理类
 */
public class ExperimentStageManager {
    private static ExperimentStage experimentStage;
    private ExperimentStageDao experimentStageDao=GreenDaoManager.getInstance().getNoCacheSession().getExperimentStageDao();
    public static final int stageTime=24*3600;//一个实验阶段的时间，单位秒

    public static final int STATUE_SUCC=0;
    public static final int STATUE_FAIL=-1;

    private static final ExperimentStageManager ourInstance = new ExperimentStageManager();

    public static ExperimentStageManager getInstance() {
        return ourInstance;
    }

    private ExperimentStageManager() {
    }

    public ExperimentStage getCurrentStage(){
        return experimentStage;
    }

    public long getStageId() {
        if(experimentStage==null){
            return -1;
        }else {
            return experimentStage.getId();
        }
    }

    //初始化实验阶段,必须先初始化项目后才能初始化实验阶段
    public void initStage(){
        experimentStage=new ExperimentStage();
        experimentStage.setStartDate(new Date());
        experimentStage.setProjectId(ProjectManager.getInstance().getProjectId());
        experimentStageDao.insert(experimentStage);
    }

    /**
     * 恢复实验阶段,必须先恢复项目后才能初始化实验阶段
     * @return STATUE_FAIL 恢复实验阶段失败,STATUE_SUCC 恢复实验阶段成功
     */
    public int resumeStage(){
        List<ExperimentStage> stageList=experimentStageDao.queryBuilder().where(ExperimentStageDao.Properties.ProjectId.eq(ProjectManager.getInstance().getProjectId())).orderDesc(ExperimentStageDao.Properties.StartDate).limit(1).list();
        if(stageList!=null&&stageList.size()>0){
            ExperimentStage stage=stageList.get(0);
            if(stage.getEndDate()==null){//这个阶段未完成
                Calendar startTime=Calendar.getInstance();
                startTime.setTime(stage.getStartDate());
                Calendar now=Calendar.getInstance();
                now.setTime(new Date());
                //项目停止的时间，毫秒
                long gap= new Date().getTime()-stage.getStartDate().getTime();
                int gapDay= (int) (gap/1000/stageTime);
                //项目停止超过一天
                if(gapDay>0){
                    stage=stage.forwardStage(gapDay);
                    stage.setStartDate(new Date());
                    experimentStageDao.insert(stage);
                }
                experimentStage=stage;
                return STATUE_SUCC;
            }else {//这个阶段已完成,进入下一个阶段
                stage=stage.getNextStage();
                experimentStage=stage;
                experimentStage.setStartDate(new Date());
                experimentStageDao.insert(experimentStage);
                return STATUE_SUCC;
//                return STATUE_FAIL;
            }
        }else {
            return STATUE_FAIL;
        }
    }

    //前进下一个实验阶段
    public void forwardStage() {
        //清除上个阶段的缓存
        GreenDaoManager.getInstance().clearAllCache();
        //先记录旧的实验阶段
        experimentStage.setEndDate(new Date());
        experimentStageDao.update(experimentStage);

        //进入下个阶段
        ExperimentStage newStage=experimentStage.getNextStage();
        newStage.setStartDate(new Date());
        experimentStageDao.insert(newStage);
        experimentStage=newStage;
    }

    //进入完成制备阶段
    public void finishCulture(){
        //清除上个阶段的缓存
        GreenDaoManager.getInstance().clearAllCache();
        //先记录旧的实验阶段
        experimentStage.setEndDate(new Date());
        experimentStageDao.update(experimentStage);

        //进入下个阶段
        ExperimentStage newStage=experimentStage.finishCulture();
        newStage.setStartDate(new Date());
        experimentStageDao.insert(newStage);
        experimentStage=newStage;
    }

    public boolean setNameForCurrentStage(String name){
        if(experimentStage!=null){
            experimentStage.setStage(name);
            experimentStageDao.update(experimentStage);
            return true;
        }
        return false;
    }
}
